package com.yaozheng.vocationaltraining.service.impl;

import java.util.HashMap;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IBaseView;
import com.yaozheng.vocationaltraining.iview.IHomeSignTaskView;
import com.yaozheng.vocationaltraining.service.HomeSignTaskService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

@EBean
public class HomeSignTaskServiceImpl implements HomeSignTaskService {
	IHomeSignTaskView iHomeSignTaskView;

	@Override
	public void init(IHomeSignTaskView iHomeSignTaskView) {
		this.iHomeSignTaskView = iHomeSignTaskView;
	}

	@Background
	@Override
	public void signRecording() {
		try {
			OkHttpClientManager.getInstance().get(
					Constants.ROOTURL + "/sign/list",
					iHomeSignTaskView.getToken(),
					new OkHttpClientManager.IBaseViewCallback(new BaseView(iHomeSignTaskView) {

						@Override
						public void responseSuccess(JSONObject jsonObject) {
							iHomeSignTaskView.signRecordingSuccess(jsonObject);
						}

						@Override
						public void responseError(String error) {
							iHomeSignTaskView.signRecordingError(error);
						}


					}));
		} catch (Exception e) {
			ErrorUtils.outErrorLog(e);
			if (iHomeSignTaskView.isResponseResult()) {
				iHomeSignTaskView
						.signRecordingError(ErrorUtils.SERVER_CONNECTION_ERROR);
			}

		}
	}

	@Background
	@Override
	public void executeSign(String day) {
		try {
			HashMap<String, String> para = new HashMap<String, String>();
			para.put("day", day);
			OkHttpClientManager.getInstance().post(Constants.ROOTURL + "/sign",
					iHomeSignTaskView.getToken(), para,
					new OkHttpClientManager.IBaseViewCallback(new BaseView(iHomeSignTaskView) {

						@Override
						public void responseSuccess(JSONObject jsonObject) {
							iHomeSignTaskView.executeSignSuccess(jsonObject);
						}

						@Override
						public void responseError(String error) {
							iHomeSignTaskView.executeSignError(error);
						}

						@Override
						public boolean isResponseResult() {
							return iHomeSignTaskView.isResponseSignResult();
						}
					}));
		} catch (Exception e) {
			ErrorUtils.outErrorLog(e);
			if (iHomeSignTaskView.isResponseSignResult()) {
				iHomeSignTaskView
						.executeSignError(ErrorUtils.SERVER_CONNECTION_ERROR);
			}

		}
	}

}
